import React from 'react';
import { WeaDialog, WeaRightMenu, WeaNewScroll, WeaAuth, WeaLocaleProvider } from "ecCom"
import { Button, } from "antd"
const getLabel = WeaLocaleProvider.getLabel;
import PrjShowGroup from "../prj-show-group"
import WeaBatchShareTable from './customerShare.js'
import './index.less'

class BatchShareDialog extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            datas: []
        };
    }
    componentWillReceiveProps(nextProps) {
        if(this.props.visible !== nextProps.visible){
            this.setState({ datas: [] }); 
        }
    }
    shouldComponentUpdate() {
        return true;
    }
    render() {
        const { visible, title, condition, onCancel } = this.props;
        const { datas } = this.state;
        const columns = [
            {
                "title": getLabel(21956, "对象类型"),
                "com": [{ label: '', type: 'INPUT', "viewAttr": 1, key: 'sharetypespan', disabled: false, },],
                "width": "20%",
                "dataIndex": "sharetypespan",
                "key": "sharetypespan"
            },
            {
                "title": getLabel(106, "对象"),
                "com": [{ label: '', type: 'INPUT', "viewAttr": 1, key: 'relatedshareidspan', disabled: false, },],
                "width": "30%",
                "dataIndex": "relatedshareidspan",
                "key": "relatedshareidspan"
            },
            {
                "title": getLabel(683, "安全级别"),
                "com": [{ label: '', type: 'INPUT', "viewAttr": 1, key: 'seclevel_seclevelMax', disabled: false, },],
                "width": "30%",
                "dataIndex": "seclevel_seclevelMax",
                "key": "seclevel_seclevelMax"
            },
            {
                "title": getLabel(3005, "共享级别"),
                "com": [{ label: '', type: 'INPUT', "viewAttr": 1, key: 'sharelevelspan', disabled: false, },],
                "width": "20%",
                "dataIndex": "sharelevelspan",
                "key": "sharelevelspan"
            },
        ];
        let that = this;
        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@ldv3sf`}
                title={title || getLabel(18645, "添加共享")}
                visible={visible}
                buttons={this.getBottomButtons()}
                icon='icon-coms-project'
                iconBgcolor='#217346'
                onCancel={() => { typeof onCancel === 'function' && onCancel(); }}
                style={{ width: 800, height: 500 }}
            >
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@cwq7dz`} datas={this.getRightMenu()}  >
                    <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@2o2rxl`} height={"100%"}>
                        <div style={{ width: "100%", height: "450px", overflow: "auto" }}>
                            <div >
                                <PrjShowGroup ecId={`${this && this.props && this.props.ecId || ''}_PrjShowGroup@02xr5r`} needTigger={true} leftComponent={getLabel(1361, "基本信息")} showGroup={true} >
                                    <WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@nup18d`}
                                        visible={true}
                                        ref="customerShare"
                                        type="form"
                                        className="prj-auth-customersize"
                                        icon={'icon-coms-fa'}
                                        iconBgcolor='#f14a2d'
                                        conditions={condition}
                                        title={getLabel(2112, "共享设置")}
                                        onOk={v => {
                                            let seclevel = v.seclevel;
                                            let seclevelMax = v.seclevelMax;
                                            if(v.sharetype === '1'||v.sharetype === '6'||v.sharetype === '7'||v.sharetype === '9'||v.sharetype === '11'){
                                            }else{
                                                if(""+seclevel&&(typeof seclevelMax === 'undefined' || seclevelMax === null || seclevelMax === "")){
                                                    v.seclevel_seclevelMax  =  "≥"+seclevel;
                                                }else if(""+seclevelMax&&(typeof seclevel === 'undefined' || seclevel === null || seclevel === "")){
                                                    v.seclevel_seclevelMax = "≤"+seclevelMax;
                                                }else{
                                                    v.seclevel_seclevelMax = seclevel+" - "+seclevelMax;
                                                }
                                            }
                                            //if (v.seclevel) { v.seclevel_seclevelMax = v.seclevel + "-" + v.seclevelMax; }
                                            if (v.sharetype !== "4") {
                                                const relatedshareids = v.relatedshareid.split(",");
                                                const relatedshareidspans = v.relatedshareidspan.split(",");
                                                let joblevel = '';
                                                let joblevelspan = '';
                                                let jobtitlespan = '';
                                                if(v.sharetype==='11'){
                                                    joblevel = v.joblevel;
                                                    joblevelspan = v.joblevelspan;
                                                    if(joblevel==='1'){
                                                        jobtitlespan = v.jobtitledepartmentspan;
                                                    }else if(joblevel==='2'){
                                                        jobtitlespan = v.jobtitlesubcompanyspan;
                                                    }
                                                }
                                                if (relatedshareids.length > 1) {
                                                    relatedshareids.map((item, index) => {
                                                        let relatedshareidspan = relatedshareidspans[index]
                                                        if(v.sharetype==='11'){
                                                            relatedshareidspan +='/'+joblevelspan;
                                                            if(joblevel==='1'||joblevel==='2'){
                                                                relatedshareidspan +='('+jobtitlespan+')';
                                                            }
                                                        }
                                                        datas.push({
                                                            ...v,
                                                            relatedshareid: item,
                                                            relatedshareidspan: relatedshareidspan
                                                        })
                                                    });
                                                }else {
                                                    let relatedshareidspan = v.relatedshareidspan;
                                                    let rolelevelspan = v.rolelevelspan;
                                                    if(v.sharetype==='11'||v.sharetype==='3'){
                                                        if(v.sharetype==='11'){
                                                            relatedshareidspan +='/'+joblevelspan;
                                                            if(joblevel==='1'||joblevel==='2'){
                                                                relatedshareidspan +='('+jobtitlespan+')';
                                                            }
                                                        }else if(v.sharetype==='3'){
                                                            relatedshareidspan = rolelevelspan+"/"+relatedshareidspan;
                                                        }                                                       
                                                        datas.push({...v,relatedshareidspan:relatedshareidspan});
                                                    }else{
                                                        datas.push(v)
                                                    }
                                                }   
                                            } else {
                                                datas.push(v)
                                            }
                                            that.setState({ datas: datas });
                                        }}
                                        onCancel={() => { }}
                                    />
                                </PrjShowGroup>
                                <WeaBatchShareTable ecId={`${this && this.props && this.props.ecId || ''}_WeaBatchShareTable@ojhwmw`}
                                    title={getLabel(1279, "共享信息")}
                                    showGroup={true}
                                    needAdd={true}
                                    needCopy={false}
                                    columns={columns}
                                    datas={datas}
                                    canAdd={true}
                                    addColumns={this.addColumns.bind(this)}
                                    onChange={this.editTableDatas.bind(this)}
                                />
                            </div>
                        </div>
                    </WeaNewScroll>
                </WeaRightMenu>
            </WeaDialog>
        )
    }
    getBottomButtons() {
        const { onCancel, onSave } = this.props;
        const { datas } = this.state;
        let btnArr = [];
        btnArr.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@eimtx3@save`} type="primary" onClick={() => { typeof onSave === 'function' && onSave(datas); }}>{getLabel(86, "保存")}</Button>)
        btnArr.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@leq4kb@close`} type="primary" onClick={() => { typeof onCancel === 'function' && onCancel(); }}>{getLabel(309, "关闭")}</Button>)
        return btnArr;
    }
    getRightMenu() {
        const { onCancel, onSave } = this.props;
        const { datas } = this.state;
        let btns = [];
        btns.push({
            key: "1",
            icon: <i className='icon-coms-Preservation' />,
            content: getLabel(86, "保存"),
            onClick: () => { typeof onSave === 'function' && onSave(datas); this.setState({ datas: [] }); }
        });
        return btns;
    }
    addColumns() {
        this.refs.customerShare.onOk();
    }
    editTableDatas(data) {
        this.setState({ datas: data })
    }

}

export default BatchShareDialog